/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bbd.crawler.job;

import bbd.crawler.helper.InitializeHelper;
import bbd.crawler.url.SimpleURLReplaceHandler;
import bbd.crawler.url.URLHandlerFactory;
import bbd.crawler.url.fwk.URLHandlerInterface;
import bbd.crawler.util.Console;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Amogh
 */
public class ThreadedJobManager {

    private ArrayList<String> artistInfoUrlList = null; // list of urls to extract artist info
    private ArrayList<String> artistNames       = null;

    private ArrayList jobsList = null; // list of jobs to setup
    
    //--------------------------------------------------------------------------
    /** Javadoc here */
    public ThreadedJobManager ( ArrayList aNames ) {
        
        artistNames = aNames;

        artistInfoUrlList = new InitializeHelper().getArtistInfoURLList();

        setupArtistInfoJobs();

    } // end default constructor

    //--------------------------------------------------------------------------
    /** Javadoc here */
    private void setupArtistInfoJobs () {

        Iterator urlIterator = artistInfoUrlList.iterator();

        while ( urlIterator.hasNext() ) {

            if ( jobsList == null ) {
                
                jobsList = new ArrayList();
            }

            try {

                String url = (String) urlIterator.next();

                URLHandlerInterface urlHandler = URLHandlerFactory.getHandlerFor (url);

                if ( urlHandler instanceof SimpleURLReplaceHandler ) {

                    ArrayList params = new ArrayList();

                    params.add (artistNames);

                    urlHandler.init ( url, params );

                    urlHandler.setSearchReplace ( " ", "_" );
                }

                jobsList.add ( 
                    new Thread (
                        new ThreadedArtistInfoJob (urlHandler)
                    )
                );
            }
            catch (ClassNotFoundException ex) {
                Console.error ( "ThreadedJobManager.setupJobs", "EXCEPTION - " + ex.getMessage() );
            }
            catch (InstantiationException ex) {
                Console.error ( "ThreadedJobManager.setupJobs", "EXCEPTION - " + ex.getMessage() );
            }
            catch (IllegalAccessException ex) {
                Console.error ( "ThreadedJobManager.setupJobs", "EXCEPTION - " + ex.getMessage() );
            }
            catch (Exception ex) {
                Console.error ( "ThreadedJobManager.setupJobs", "EXCEPTION - " + ex.getMessage() );
            }
        }

    } // end method setupArtistInfoJobs

    //--------------------------------------------------------------------------
    /** Javadoc here */
    public void startJobs () {

        System.out.println("JOB list :");

        Iterator jobIterator = jobsList.iterator();

        while ( jobIterator.hasNext() ) {

            ( (Thread) jobIterator.next() ).start();
        }

    } // end method startJobs

} // end class ThreadedJobManager
